package com.cn.ccp.action.course;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.CourseForumBean;
import com.cn.ccp.util.CCPContants;

/**
 * 课程论坛信息回复
 * @author Administrator
 *
 */
public class CourseForumReplyManagerAction extends BaseAction{
	
	private Log log = LogFactory.getLog(CourseForumReplyManagerAction.class);
	private List<CourseForumBean> list ;
	private CourseForumBean bean ;
	
	private String contents ;
	private String id ;
	private String replyInfo ;
	private String courseId,selectedCourseId;
	private String userImage;
	private boolean hasImage;
	
		
	public String showContent(){
		FacesContext context = FacesContext.getCurrentInstance();
		id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		String type = (String)context.getExternalContext().getRequestParameterMap().get("type");
		courseId = (String)context.getExternalContext().getRequestParameterMap().get("courseId");
		selectedCourseId = (String)context.getExternalContext().getRequestParameterMap().get("selectedCourseId");
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute("bbs-type", type);
		bean = getCourseForumService().showBean(id);
		String image = getUserHeadImage(bean.getUserId());
		if("".equalsIgnoreCase(image))
			userImage = "images/teacher.png";
		else{
			userImage = "tempfile/"+bean.getUserId()+"/"+image;
			hasImage = true;
		}
		log.info("---------------------userImage = "+userImage);
		getCourseForumService().look(id);
		setCurrentPage(1);
		contents = "";
		
		if(bean.getUserId().equals(CCPContants.getCurrUserId()))
			bean.setShowEdit(true);
		getpagedetail();
		return "look";
	}
	
	
	public void deleteReply(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		log.info("-------------------------id = "+id);
		getCourseForumService().deleteReplyForum(id);
		getpagedetail();
	}
	
	private String getUserHeadImage(String userId){
		File des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+ userId);
		StringBuffer buf = new StringBuffer();
		if(des.isDirectory()){
			File[] files = des.listFiles();
			for(File f : files){
				if(f.getName().indexOf("head-image") != -1){
					buf.append(f.getName());
					break;
				}
			}
		}
//		log.info(buf.toString());
		return buf.toString();
	}
	
	
	public String reply(){
		FacesContext context = FacesContext.getCurrentInstance();
		contents = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("content1").toString();
		if(contents == null || contents.equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_2")));
			return "";
		}
		bean = getCourseForumService().showBean(id);
		getCourseForumService().install(bean.getCourseId() ,bean.getTitle(), contents, CCPContants.getCurrUserId() ,2 ,id);
		getCourseForumService().reply(id);
		
		
		contents = "";
		context.addMessage("formid", new FacesMessage(getMessages("label_success")));
		getpagedetail();
		return "";
	}
	
	public String goback(){
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		String formPage = (String) session.getAttribute("bbs-type");
		if(formPage != null  && !formPage.equals("")){
			return formPage;
		}
		return "main";
	}
	
	
	public void disableContent(){
		FacesContext context = FacesContext.getCurrentInstance();
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		log.info("----------------------- id = "+id);
		getCourseForumService().disableContent(id);
		getpagedetail();
	}
	
	public void enableContent(){
		FacesContext context = FacesContext.getCurrentInstance();
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		log.info("----------------------- id = "+id);
		getCourseForumService().enableContent(id);
		getpagedetail();
	}
	
	
	@Override
	public void getpagedetail() {
		setItemCount(getCourseForumService().getReplyCountManager(id));
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		list = getCourseForumService().replyPageQueryManager(start, pagesize ,id);
		for(CourseForumBean b:list){
//			log.info("-----------------:"+b.getUserId());
			String image = getUserHeadImage(b.getUserId());
			if("".equalsIgnoreCase(image))
				b.setUserHeadImage( "images/teacher.png");
			else
				b.setUserHeadImage( "/tempfile/"+b.getUserId()+"/"+image);
//			log.info("-----------------:"+b.getUserHeadImage());
		}
	}
	

	public List<CourseForumBean> getList() {
		return list;
	}

	public void setList(List<CourseForumBean> list) {
		this.list = list;
	}

	public CourseForumBean getBean() {
		if(id != null && !id.equals("")){
			bean = getCourseForumService().showBean(id);
			if(bean.getUserId().equals(CCPContants.getCurrUserId()))
				bean.setShowEdit(true);
		}
		return bean;
	}

	public void setBean(CourseForumBean bean) {
		this.bean = bean;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}

	public String getReplyInfo() {
		getpagedetail();
		return replyInfo;
	}

	public void setReplyInfo(String replyInfo) {
		this.replyInfo = replyInfo;
	}

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public String getSelectedCourseId() {
		return selectedCourseId;
	}

	public void setSelectedCourseId(String selectedCourseId) {
		this.selectedCourseId = selectedCourseId;
	}

	public String getUserImage() {
		log.info("-----------------userImage = "+userImage);
		return userImage;
	}

	public void setUserImage(String userImage) {
		this.userImage = userImage;
	}


	public boolean isHasImage() {
		return hasImage;
	}


	public void setHasImage(boolean hasImage) {
		this.hasImage = hasImage;
	}

	
}
